/*
* sop设备表模块
*/
const express = require('express');
const router = express.Router();
// 引入数据库
const db = require("../../config/db");
// 引入时间包
const moment = require("moment");
// 引入方法库
const { toCamelCase, toUnderlineCase, verifyToken, recursionGenerateTree } = require("../../config/utils");
// 引入日志
const log = require("../log");

// 新增*sop设备*
router.post("/addDevice", (req, res) => {
    console.log("=========== 新增*sop设备*接口============");
    // 获取参数
    let { deptName, parentId, status, orderNum } = req.body;
    // 获取创建人
    let { username } = verifyToken(req.headers.authorization);
    // 获取创建时间
    let createTime = moment().format("YYYY-MM-DD HH:mm:ss");
    // 判断当前项目和设备名称是否存在
    let sql_check = `select * from sop_device where (name = '${deptName}' and project_id = '${parentId}')`;
    db.query("判断当前项目和设备名称是否存在", sql_check, (err, sql_check_result) => {
        if (err) {
            res.send({ code: 500, msg: "判断当前项目和设备名称是否存在失败", data: err });
            return;
        }
        if (sql_check_result.length > 0) {
            res.send({ code: 500, msg: "项目名称已存在相同设备名称，请重新输入！" });
            return;

        }
        // 插入数据库
        let sql = `insert into sop_device (name, project_id, status, create_by, create_time, order_num) values 
        ('${deptName}', '${parentId}', '${status}', '${username}', '${createTime}', '${orderNum}')`;
        db.query("新增*sop设备*", sql, (err1, result) => {
            if (err1) {
                res.send({ code: 500, msg: "新增*sop设备*失败", data: err1 });
                return;
            }
            if (result.affectedRows > 0) {
                res.send({ code: 200, msg: "新增*sop设备*成功" });
            } else {
                res.send({ code: 500, msg: "新增*sop设备*失败" });
            }
        })
    })
})

// 修改*sop设备*
router.put("/updateDevice", (req, res) => {
    console.log("=========== 修改*sop设备*接口============");
    // 获取参数
    let { deptName, parentId, status, orderNum, deptId } = req.body;
    // 截取id字段
    deptId = deptId.split("-")[1];
    // 获取创建人
    let { username } = verifyToken(req.headers.authorization);
    // 获取创建时间
    let createTime = moment().format("YYYY-MM-DD HH:mm:ss");
    // 判断当前项目和设备名称是重复
    let sql_check = `select * from sop_device where (name = '${deptName}' and project_id = '${parentId}') and id != '${deptId}'`;
    db.query("修改*sop设备*判断当前项目和设备名称是重复", sql_check, (err, sql_check_result) => {
        if (err) {
            res.send({ code: 500, msg: "修改*sop设备*判断当前项目和设备名称是重复失败", data: err });
            return;
        }
        if (sql_check_result.length > 0) {
            res.send({ code: 500, msg: "当前项目已存在相同设备名称，请重新输入！" });
            return;
        }
        // 修改数据库
        let sql = `update sop_device set name = '${deptName}', project_id = '${parentId}', status = '${status}', update_by = '${username}', 
        update_time = '${createTime}', order_num = '${orderNum}' where id = '${deptId}'`;
        db.query("修改*sop设备*", sql, (err1, result) => {
            if (err1) {
                res.send({ code: 500, msg: "修改*sop设备*失败", data: err1 });
                return;
            }
            if (result.affectedRows > 0) {
                res.send({ code: 200, msg: "修改*sop设备*成功" });
            } else {
                res.send({ code: 500, msg: "修改*sop设备*失败" });
            }
        })
    })
})

// 删除*sop设备表*
router.delete("/deleteDevice/:deviceId", (req, res) => {
    console.log("=========== 删除*sop设备表*接口============");
    // 获取参数
    let { deviceId } = req.params;
    // 先删除设备与组件关联表  再 删除设备表
    let sql_device_components = `delete from sop_device_sop_components where device_id = '${deviceId}'`;
    db.query("删除*sop设备与组件关联表*", sql_device_components, (err, result) => {
        if (err) {
            res.send({ code: 500, msg: "删除*sop设备与组件关联表*失败", data: err });
            return;
        }
        if (result.affectedRows > 0) {
            res.send({ code: 200, msg: "删除*sop设备与组件关联表*成功" });
        } else {
            res.send({ code: 200, msg: "当前设备无关联组件，删除*sop设备与组件关联表*成功" });
        }
        // 删除设备表
        let sql_device = `delete from sop_device where id = '${deviceId}'`;
        db.query("删除*sop设备表*", sql_device, (err, result) => {
            if (err) {
                res.send({ code: 500, msg: "删除*sop设备表*失败", data: err });
                console.log("删除*sop设备表*失败")
                console.log(err);
                return;
            }
        })
    })
})

module.exports = router;